]>
Commit | Line | Data |
---|---|---|
f8aec187 BB |
1 | using System; |
2 | using System.Collections.Generic; | |
3 | using System.Linq; | |
4 | using System.Text; | |
5 | using Microsoft.Xna.Framework; | |
6 | using Microsoft.Xna.Framework.Content; | |
7 | using Microsoft.Xna.Framework.Graphics; | |
8 | ||
9 | namespace SuperPolarity | |
10 | { | |
11 | class Actor | |
12 | { | |
74c15570 | 13 | protected SuperPolarity game; |
2af83e98 | 14 | |
38c7d3f9 BB |
15 | public List<Actor> Children; |
16 | ||
f8aec187 BB |
17 | // Graphics / In-Game |
18 | protected Texture2D Texture; | |
19 | protected Vector2 Origin; | |
20 | public bool Active; | |
74c15570 BB |
21 | public Rectangle Box; |
22 | protected Vector4 BoxDimensions; | |
23 | protected Texture2D BoxTexture; | |
f8aec187 BB |
24 | |
25 | // Physical Properties | |
2af83e98 | 26 | public Vector2 Position; |
f8aec187 BB |
27 | protected Vector2 Velocity; |
28 | protected Vector2 Acceleration; | |
38c7d3f9 | 29 | public float Angle; |
f8aec187 BB |
30 | |
31 | // Constraints / Behavior | |
32 | protected float MaxVelocity; | |
33 | protected float AccelerationRate; | |
74c15570 BB |
34 | protected int HP; |
35 | protected bool Immortal; | |
36 | public bool Dying; | |
37 | public int Value; | |
38 | protected Color Color; | |
39 | ||
40 | public Actor Parent; | |
f8aec187 BB |
41 | |
42 | public int Width | |
43 | { | |
44 | get { return Texture.Width; } | |
45 | } | |
46 | ||
47 | public int Height | |
48 | { | |
49 | get { return Texture.Height; } | |
50 | } | |
51 | ||
74c15570 | 52 | public Actor(SuperPolarity newGame) |
2af83e98 BB |
53 | { |
54 | game = newGame; | |
55 | } | |
56 | ||
57 | public virtual void Initialize(Texture2D texture, Vector2 position) | |
f8aec187 BB |
58 | { |
59 | Texture = texture; | |
60 | Position = position; | |
61 | Active = true; | |
62 | ||
38c7d3f9 BB |
63 | Children = new List<Actor>(); |
64 | ||
f8aec187 BB |
65 | Origin = new Vector2(Texture.Width / 2, Texture.Height / 2); |
66 | Velocity = new Vector2(0, 0); | |
67 | Acceleration = new Vector2(0, 0); | |
68 | ||
69 | MaxVelocity = 5; | |
70 | AccelerationRate = 10; | |
74c15570 BB |
71 | |
72 | HP = 1; | |
73 | Immortal = false; | |
74 | BoxDimensions.X = 20; | |
75 | BoxDimensions.Y = 20; | |
76 | BoxDimensions.W = 20; | |
77 | BoxDimensions.Z = 20; | |
78 | ||
79 | Dying = false; | |
80 | Value = 1; | |
81 | ||
82 | InitBox(); | |
83 | BoxTexture = new Texture2D(game.GraphicsDevice, 1, 1); | |
84 | BoxTexture.SetData(new Color[] { Color.White }); | |
85 | ||
86 | Color = Color.White; | |
87 | } | |
88 | ||
89 | protected void InitBox() | |
90 | { | |
91 | Box = new Rectangle((int)(Position.X - BoxDimensions.X), (int)(Position.Y - BoxDimensions.X), (int)(BoxDimensions.X + BoxDimensions.W), (int)(BoxDimensions.Y + BoxDimensions.Z)); | |
f8aec187 BB |
92 | } |
93 | ||
94 | public void AutoDeccelerate(GameTime gameTime) | |
95 | { | |
96 | if (Acceleration.X == 0 && Velocity.X > 0) | |
97 | { | |
98 | if (AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds > Velocity.X) | |
99 | { | |
100 | Velocity.X = 0; | |
101 | Acceleration.X = 0; | |
102 | } | |
103 | else | |
104 | { | |
105 | Acceleration.X = -AccelerationRate; | |
106 | } | |
107 | } | |
108 | ||
109 | if (Acceleration.X == 0 && Velocity.X < 0) | |
110 | { | |
111 | if (-AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds < Velocity.X) | |
112 | { | |
113 | Velocity.X = 0; | |
114 | Acceleration.X = 0; | |
115 | } | |
116 | else | |
117 | { | |
118 | Acceleration.X = AccelerationRate; | |
119 | } | |
120 | } | |
121 | ||
122 | if (Acceleration.Y == 0 && Velocity.Y > 0) | |
123 | { | |
124 | if (AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds > Velocity.Y) | |
125 | { | |
126 | Velocity.Y = 0; | |
127 | Acceleration.Y = 0; | |
128 | } | |
129 | else | |
130 | { | |
131 | Acceleration.Y = -AccelerationRate; | |
132 | } | |
133 | } | |
134 | ||
135 | if (Acceleration.Y == 0 && Velocity.Y < 0) | |
136 | { | |
137 | if (-AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds < Velocity.Y) | |
138 | { | |
139 | Velocity.Y = 0; | |
140 | Acceleration.Y = 0; | |
141 | } | |
142 | else | |
143 | { | |
144 | Acceleration.Y = AccelerationRate; | |
145 | } | |
146 | } | |
147 | } | |
148 | ||
149 | public virtual void Update(GameTime gameTime) | |
150 | { | |
151 | Move(gameTime); | |
152 | ChangeAngle(); | |
38c7d3f9 | 153 | CheckOutliers(); |
74c15570 BB |
154 | UpdateBox(); |
155 | } | |
156 | ||
157 | protected virtual void UpdateBox() | |
158 | { | |
159 | Box.X = (int)(Position.X - BoxDimensions.X); | |
160 | Box.Y = (int)(Position.Y - BoxDimensions.Y); | |
f8aec187 BB |
161 | } |
162 | ||
38c7d3f9 | 163 | public virtual void Move(GameTime gameTime) |
f8aec187 BB |
164 | { |
165 | AutoDeccelerate(gameTime); | |
166 | ||
38c7d3f9 BB |
167 | var maxVelocity = MaxVelocity; |
168 | ||
f8aec187 BB |
169 | Velocity.X = Velocity.X + Acceleration.X * (float)gameTime.ElapsedGameTime.TotalSeconds; |
170 | Velocity.Y = Velocity.Y + Acceleration.Y * (float)gameTime.ElapsedGameTime.TotalSeconds; | |
171 | ||
172 | if (Velocity.X > MaxVelocity) | |
173 | { | |
174 | Velocity.X = MaxVelocity; | |
175 | } | |
176 | ||
177 | if (Velocity.X < -MaxVelocity) | |
178 | { | |
179 | Velocity.X = -MaxVelocity; | |
180 | } | |
181 | ||
182 | if (Velocity.Y > MaxVelocity) | |
183 | { | |
184 | Velocity.Y = MaxVelocity; | |
185 | } | |
186 | ||
187 | if (Velocity.Y < -MaxVelocity) | |
188 | { | |
189 | Velocity.Y = -MaxVelocity; | |
190 | } | |
191 | ||
192 | Position.X = Position.X + Velocity.X; | |
193 | Position.Y = Position.Y + Velocity.Y; | |
194 | } | |
195 | ||
196 | public void ChangeAngle() | |
197 | { | |
198 | Angle = (float)Math.Atan2(Velocity.Y, Velocity.X); | |
199 | } | |
200 | ||
201 | public virtual void Draw(SpriteBatch spriteBatch) | |
202 | { | |
38c7d3f9 BB |
203 | foreach (Actor child in Children) |
204 | { | |
205 | child.Draw(spriteBatch); | |
206 | } | |
207 | ||
74c15570 BB |
208 | spriteBatch.Draw(Texture, Position, null, Color, Angle, Origin, 1f, SpriteEffects.None, 0f); |
209 | spriteBatch.Draw(BoxTexture, Box, new Color(255, 0, 255, 25)); | |
f8aec187 | 210 | } |
38c7d3f9 BB |
211 | |
212 | void CheckOutliers() | |
213 | { | |
214 | for (var i = Children.Count; i > 0; i--) | |
215 | { | |
216 | var actor = Children[i - 1]; | |
217 | if (actor.Position.X < -SuperPolarity.OutlierBounds || actor.Position.Y < -SuperPolarity.OutlierBounds || | |
218 | actor.Position.X > game.GraphicsDevice.Viewport.Width + SuperPolarity.OutlierBounds || | |
219 | actor.Position.Y > game.GraphicsDevice.Viewport.Height + SuperPolarity.OutlierBounds) | |
220 | { | |
221 | Children.Remove(actor); | |
222 | } | |
223 | } | |
224 | } | |
74c15570 BB |
225 | |
226 | public virtual void Collide(Actor other, Rectangle collision) | |
227 | { | |
228 | } | |
229 | ||
230 | public void TakeDamage(int amount) | |
231 | { | |
232 | if (!Immortal) | |
233 | { | |
234 | HP = HP - amount; | |
235 | if (HP < 0) | |
236 | { | |
237 | Die(); | |
238 | } | |
239 | } | |
240 | } | |
241 | ||
242 | protected virtual void Die() | |
243 | { | |
244 | Dying = true; | |
245 | } | |
f8aec187 BB |
246 | } |
247 | } |